import re
from lxml import etree


def parse_html(page_source):
    html = etree.HTML(page_source)
    externalId = "外部简历唯一标识"
    externalMsg = "外部简历下载信息"
    sourceSub = "智联招聘"
    try:
        applyDepartment = html.xpath('//*[@id="resumeDetail"]/div[1]/div/dl/dd[6]/text()')[0]
    except:
        applyDepartment = 'N/A'
    try:
        applyPosition = html.xpath('//*[@id="resumeDetail"]/div[1]/div/dl/dd[5]/text()')[0]
    except:
        applyPosition = 'N/A'
    try:
        applyDateStr = html.xpath('//*[@id="resume-detail-wrapper"]/div[1]/div[1]/span/span[2]/text()')[0][4:-5]
    except:
        applyDateStr = 'N/A'
    try:
        updateDateStr = html.xpath('//*[@id="resume-detail-wrapper"]/div[1]/div[1]/span/span[2]/text()')[0][4:-5]
    except:
        updateDateStr = 'N/A'

    try:
        tags_list = html.xpath('//p[@class="is-weight-bold"]/text()')
        tags = ' '
        for i in tags_list:
            tags += i + ','
    except:
        tags = 'N/A'

    try:
        status = html.xpath('//*[@id="resumeDetail"]/div[1]/div/dl/dd[3]/text()')[0]
    except:
        status = 'N/A'
    try:
        name = html.xpath('//*[@id="resume-detail-wrapper"]/div[1]/div[2]/div/div[1]/div[1]/span[1]/text()')[0]
    except:
        name = 'N/A'
    try:
        sex = html.xpath('//*[@id="resume-detail-wrapper"]/div[1]/div[2]/div/div[1]/p[1]/span[1]/text()')[0]
    except:
        sex = 'N/A'
    mobile = '***********'
    email = '***********'

    try:
        birthdayStr = html.xpath('//*[@id="resume-detail-wrapper"]/div[1]/div[2]/div/div[1]/p[1]/span[2]/text()')[0]
    except:
        birthdayStr = 'N/A'
    try:
        currentAddress = \
            html.xpath('//*[@id="resume-detail-wrapper"]/div[1]/div[2]/div/div[1]/p[2]/span[1]/span[2]/text()')[0]
    except:
        currentAddress = 'N/A'
    try:
        currentCity = html.xpath('//*[@id="resume-detail-wrapper"]/div[1]/div[2]/div/div[1]/p[2]/span[1]/span[2]/text()')[0]
    except:
        currentCity = 'N/A'
    currentSalary = 'N/A'
    salaryStart = 'N/A'
    salaryEnd = 'N/A'

    try:
        workYear = \
            re.findall('\d+',
                       html.xpath('//*[@id="resume-detail-wrapper"]/div[1]/div[2]/div/div[1]/p[1]/span[3]/text()')[0])[0]
    except:
        workYear = 'N/A'
    try:
        workLastPosition = html.xpath('string(//*[@id="resumeDetail"]/div[3]/ul/li[1]/p[2]/span[1])')
    except:
        workLastPosition = 'N/A'
    try:
        workLastCompany = html.xpath('string(//*[@id="resumeDetail"]/div[3]/ul/li[1]/p[1]/span[2])')
    except:
        workLastCompany = 'N/A'
    try:
        eduLastEducation = html.xpath('//*[@id="resumeDetail"]/div[2]/ul/li/p/span[4]/text()')[0]
    except:
        eduLastEducation = 'N/A'
    try:
        eduLastSchool = html.xpath('//*[@id="resumeDetail"]/div[2]/ul/li/p/span[2]/text()')[0]
    except:
        eduLastSchool = 'N/A'
    try:
        eduLastSchoolTag = html.xpath('//*[@id="resumeDetail"]/div[2]/ul/li/p/span[2]/text()')[0]
    except:
        eduLastSchoolTag = 'N/A'
    try:
        eduLastMajor = html.xpath('//*[@id="resumeDetail"]/div[2]/ul/li/p/span[3]/text()')[0]
    except:
        eduLastMajor = 'N/A'
    try:
        expectJob = html.xpath('//*[@id="resumeDetail"]/div[1]/div/dl/dd[6]/text()')[0]
    except:
        expectJob = 'N/A'
    try:
        expectPay = html.xpath('//*[@id="resumeDetail"]/div[1]/div/dl/dd[2]/text()')[0]
    except:
        expectPay = 'N/A'

    if expectPay == '面议':
        expectPayStart = '面议'
        expectPayEnd = '面议'
    elif expectPay == 'N/A':
        expectPayStart = 'N/A'
        expectPayEnd = 'N/A'
    else:
        try:
            expectPayStart = html.xpath('//*[@id="resumeDetail"]/div[1]/div/dl/dd[2]/text()')[0].split('-')[0]
            expectPayEnd = html.xpath('//*[@id="resumeDetail"]/div[1]/div/dl/dd[2]/text()')[0].split('-')[1][:-3]
        except:
            expectPayStart = 'N/A'
            expectPayEnd = 'N/A'

    try:
        workPlace = html.xpath('//*[@id="resumeDetail"]/div[1]/div/dl/dd[1]/text()')[0]
    except:
        workPlace = 'N/A'
    try:
        workIndustry = html.xpath('//*[@id="resumeDetail"]/div[1]/div/dl/dd[6]/text()')[0]
    except:
        workIndustry = 'N/A'
    try:
        workNature = html.xpath('//*[@id="resumeDetail"]/div[1]/div/dl/dd[4]/text()')[0]
    except:
        workNature = 'N/A'
    entryDate = 'N/A'
    keyword = tags
    try:
        evaluation = html.xpath('string(//div[@class="resume-content__section-body resume-content__section--paragraph"])')
    except:
        evaluation = 'N/A'

    # 工作经历
    works = []
    try:
        for i in range(1,10):
            company = html.xpath('string(//div[@class="resume-content__section"]/p/span[contains(text(),"工作经历")]/..//following-sibling::ul/li[{}]/p[1]/span[2])'.format(i))
            position = html.xpath('string(//div[@class="resume-content__section"]/p/span[contains(text(),"工作经历")]/..//following-sibling::ul/li[{}]/p[2]/span[1])'.format(i))
            duty = html.xpath('string(//div[@class="resume-content__section"]/p/span[contains(text(),"工作经历")]/..//following-sibling::ul/li[{}]/dl/dd)'.format(i))
            startDate = html.xpath('string(//div[@class="resume-content__section"]/p/span[contains(text(),"工作经历")]/..//following-sibling::ul/li[{}]/p[1]/span[1]/span[1])'.format(i))
            endDate = html.xpath('string(//div[@class="resume-content__section"]/p/span[contains(text(),"工作经历")]/..//following-sibling::ul/li[{}]/p[1]/span[1]/span[3])'.format(i))
            if company == position == duty:
                break
            else:
                work = {"company": company,
                        "position": position,
                        "duty": duty,
                        "startDate": startDate,
                        "endDate": endDate}
                works.append(work)
    except:
        pass
    # 项目经历
    projects = []
    try:
        for i in range(1,10):
            projectName = html.xpath('string(//div[@class="resume-content__section"]/p/span[contains(text(),"项目经历")]/..//following-sibling::ul/li[{}]/p/span[2])'.format(i))
            duty = html.xpath('string(//div[@class="resume-content__section"]/p/span[contains(text(),"项目经历")]/..//following-sibling::ul/li[{}]/dl/dd[1])'.format(i))
            description = html.xpath('string(//div[@class="resume-content__section"]/p/span[contains(text(),"项目经历")]/..//following-sibling::ul/li[{}]/dl/dd[2])'.format(i))
            startDate = html.xpath('string(//div[@class="resume-content__section"]/p/span[contains(text(),"项目经历")]/..//following-sibling::ul/li[{}]/p/span[1]/span[1])'.format(i))
            endDate = html.xpath('string(//div[@class="resume-content__section"]/p/span[contains(text(),"项目经历")]/..//following-sibling::ul/li[{}]/p/span[1]/span[3])'.format(i))
            if projectName==duty==description:
                break
            project = {
                "projectName": projectName,
                "duty": duty,
                "description": description,
                "startDate": startDate,
                "endDate": endDate}
            projects.append(project)
    except:
        pass
    # 教育经历
    educations = []
    try:
        for i in range(1,10):
            school = html.xpath('string(//div[@class="resume-content__section"]/p[contains(text(),"教育经历")]/..//following-sibling::ul/li[{}]/p/span[2])'.format(i))
            major = html.xpath('string(//div[@class="resume-content__section"]/p[contains(text(),"教育经历")]/..//following-sibling::ul/li[{}]/p/span[3])'.format(i))
            degree = html.xpath('string(//div[@class="resume-content__section"]/p[contains(text(),"教育经历")]/..//following-sibling::ul/li[{}]/p/span[4])'.format(i))
            startDate = html.xpath('string(//div[@class="resume-content__section"]/p[contains(text(),"教育经历")]/..//following-sibling::ul/li[{}]/p/span[1]/span[1])'.format(i))
            endDate = html.xpath('string(//div[@class="resume-content__section"]/p[contains(text(),"教育经历")]/..//following-sibling::ul/li[{}]/p/span[1]/span[3])'.format(i))
            if school==major==degree:
                break
            education = {
                "school": school,
                "major": major,
                "degree": degree,
                "startDate": startDate,
                "endDate": endDate}
            educations.append(education)
    except:
        pass

    user_info = {
        "externalId": externalId,
        "externalMsg": externalMsg,
        "sourceSub": sourceSub,
        "applyDepartment": applyDepartment,
        "applyPosition": applyPosition,
        "applyDateStr": applyDateStr,
        "updateDateStr": updateDateStr,
        "tags": tags,
        "status": status,
        "basicInfo": {
            "name": name,
            "sex": sex,
            "mobile": mobile,
            "email": email,
            "birthdayStr": birthdayStr,
            "currentAddress": currentAddress,
            "currentCity": currentCity,
            "currentSalary": currentSalary,
            "salaryStart": salaryStart,
            "salaryEnd": salaryEnd,
            "workYear": workYear,
            "workLastPosition": workLastPosition,
            "workLastCompany": workLastCompany,
            "eduLastEducation": eduLastEducation,
            "eduLastSchool": eduLastSchool,
            "eduLastSchoolTag": eduLastSchoolTag,
            "eduLastMajor": eduLastMajor,
        },
        "jobIntention": {
            "expectJob": expectJob,
            "expectPay": expectPay,
            "expectPayStart": expectPayStart,
            "expectPayEnd": expectPayEnd,
            "workPlace": workPlace,
            "workIndustry": workIndustry,
            "workNature": workNature,
            "entryDate": entryDate,
            "keyword": keyword
        },
        "selfEvaluation": {
            "evaluation": evaluation
        },
        "works": works,
        "projects": projects,
        "educations": educations
    }
    return user_info